<?php

/*

CREATE TABLE `nested_tree` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `leftKey` int(8) unsigned NOT NULL,
  `rightKey` int(8) unsigned NOT NULL,
  `level` int(8) unsigned NOT NULL,
  `treeId` int(8) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 CREATE TABLE `categories` (
  `id` int(8) NOT NULL,
  `name` varchar(32) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 */

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'     => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'zend_db_tree'
));
Zend_Db_Table::setDefaultAdapter($db);

/**
 * Categories table data gateway
 */
class Default_Model_DbTable_Categories extends Zend_Db_Tree_Nested_Table
{
    /**
     * Table
     * @var string
     */
    protected $_name = 'categories';

//    /**
//     * Key
//     * @var string
//     */
//    protected $_primary = 'id';

    /**
     * Categories Tree Id
     * @var integer
     */
    protected $_nestedTreeId = 1;

}

// Настройка колонок в твоей таблице для деревьев.
$columns = array(
    Zend_Db_Tree_Nested::ID => 'id',
    Zend_Db_Tree_Nested::LEFT_KEY => 'leftKey',
    Zend_Db_Tree_Nested::RIGHT_KEY => 'rightKey',
    Zend_Db_Tree_Nested::LEVEL => 'level',
    Zend_Db_Tree_Nested::TREE_ID => 'treeId',
);

// Алиасы совпадают с колонками
Zend_Db_Tree_Nested_Adapter_Abstract::setDefaultAliases($columns);
Zend_Db_Tree_Nested_Adapter_Abstract::setDefaultColumns($columns);
// Таблица с деревьями
Zend_Db_Tree_Nested_Adapter_Abstract::setDefaultTableName('nested_tree');
// Ключ по которому можно будет удалить данные из связанной таблицы с данными
Zend_Db_Tree_Nested_Adapter_Abstract::setDefaultRelativeKey('id');

// Clear tables
$db->query('TRUNCATE ' . Zend_Db_Tree_Nested_Adapter_Abstract::getDefaultTableName());
$db->query('TRUNCATE categories');

$categories = new Default_Model_DbTable_Categories();

// Data
$animals = array('name' => 'Animals');
    $cats = array('name' => 'Cats');
        $siamese = array('name' => 'Siamese');
        $persian = array('name' => 'Persian');
    $dogs = array('name' => 'Dogs');
        $layka = array('name' => 'Layka');
        $basenji = array('name' => 'Basenji');
$nature = array('name' => 'Nature');
    $wood = array('name' => 'Wood');
    $sea = array('name' => 'Sea');
    $flowers = array('name' => 'Flowers');
    $desert = array('name' => 'Desert');

// Create tree in DB
$animalsId = $categories->insert($animals);
    $catsId = $categories->insertChildLast($cats, $animalsId);
        $categories->insertChildLast($siamese, $catsId);
        $categories->insertChildFirst($persian, $catsId);
    $dogsId = $categories->insertChildFirst($dogs, $animalsId);
        $categories->insertChildLast($layka, $dogsId);
        $categories->insertChildFirst($basenji, $dogsId);
$natureId = $categories->insert($dogs);
    $categories->insertChildLast($wood, $natureId);
    $categories->insertChildLast($sea, $natureId);
    $categories->insertChildLast($flowers, $natureId);
    $categories->insertChildLast($desert, $natureId);

// Fetch open tree
$tree = $categories->fetchOpenTree();
